



import 'package:flutter/material.dart';


main()=> runApp(CLMyapp());

class CLMyapp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        home: CLbody()
    );
  }
}


class CLbody extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CLbodystate();
  }
}

class _CLbodystate extends State<CLbody> {

  List<Widget> widgets=[CLhomecontent()];//不能定义成var，否则添加进数组的不能是widget,不能定义在build里面，否则build重画会被初始化
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return
      Scaffold(
          appBar: AppBar(
            title:Text("计数器") ,
          ),
          body:  Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: widgets,
            ),
          ),
          floatingActionButton: RaisedButton(
              child: Text("添加hi"),
              onPressed: (){
                setState(() {
                  widgets.insert(0, Text("hi"));

                });
              })
      );
  }

}
class CLhomecontent extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return CLhomecontentstate();
  }
}

class CLhomecontentstate extends State<CLhomecontent>{
  int _count = 0;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: [
        _getbutton(),
        Text("当前计数$_count",
          style: TextStyle(color: Colors.orange, fontSize: 30),
        ),
      ],
    );
  }

  Widget _getbutton() {//注意，这个方法不能放到类外面了
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [

        RaisedButton(
            child: Text("+",style: TextStyle(fontSize: 20),),
            color: Colors.pink,
            onPressed: () {

              setState(() {
                _count++;
              });
            }
        ),
        RaisedButton(
            child: Text("-",style: TextStyle(fontSize: 20),),
            color: Colors.deepOrange,
            onPressed: () { setState(() {
              _count--;
            });})
      ],
    );
  }
}
